﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTS = Microsoft.SqlServer.Dts.Runtime;
using System.Collections;

namespace Vista.Seguridad
{
    public partial class FormRespaldo : Form
    {
       

        public FormRespaldo()
        {
            InitializeComponent();
            //otherwise will throw an exception when calling ReportProgress method
            backgroundWorker1.WorkerReportsProgress = true;
            //mandatory. Otherwise we would get an InvalidOperationException when trying to cancel the operation
            backgroundWorker1.WorkerSupportsCancellation = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void btoCancel_Click(object sender, EventArgs e)
        {
            backgroundWorker1.CancelAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            DTS.Package dtsPackage = null;
            DTS.Application dtsApplication = null;
            dtsApplication = new DTS.Application();
            dtsPackage = dtsApplication.LoadPackage(@"C:\Users\lucas.farruggia\Desktop\Sistema_20120910\04.Backup\JobBacukpUnipan\JobBacukpUnipan\Package.dtsx", null);
            DTS.DTSExecResult packageResult = dtsPackage.Execute();

            DateTime start = DateTime.Now;
            e.Result = "";
            
            for (int i = 0; i < 100; i++)
            {
                System.Threading.Thread.Sleep(50); 
                backgroundWorker1.ReportProgress(i, DateTime.Now);

                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
            }
            TimeSpan duration = DateTime.Now - start;
            e.Result = "Duracion: " + duration.TotalMilliseconds.ToString() + " ms.";
            
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage; //update progress bar
            DateTime time = Convert.ToDateTime(e.UserState); //get additional information about progress
            //in this example, we log that optional additional info to textbox
            txtOutput.AppendText(time.ToLongTimeString());
            txtOutput.AppendText(Environment.NewLine);
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                MessageBox.Show("La Tarea fue cancelada");
            }
            else if (e.Error != null)
            {
                MessageBox.Show("Error. Detalle: " + (e.Error as Exception).ToString());
            }
            else
            {
                MessageBox.Show("La Tarea se ejecuto con Exito: " + e.Result.ToString());
                progressBar1.Value = progressBar1.Minimum;
                txtOutput.Text = "";
            }
        }
    }
}
